
package com.fire.eyes.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fire.core.service.ITMitServiceDicBaseServices;
import com.fire.core.util.BlogUtil;
import com.fire.core.util.XmlUtil;
import com.fire.eyes.constants.JkConstants;
import com.fire.eyes.request.ReqDTO;
import com.fire.eyes.request.element.XmlMessageDTO;

/**
 * 类描述：jk配置工具类
 * @author: yangxiaotian
 *  @date： 日期：2018年5月5日 时间：下午3:43:37
 * @version 1.0
 */
@Component
public class JkUtil {
    private final static Logger blogout = Logger.getLogger(JkUtil.class);
    @Autowired
    private ITMitServiceDicBaseServices baseServices;

    private  String PARTNER_CODE = "";
    private  String URL = "";
    private  String MD5_KEY = "";
    
    /**
     * 获取配置信息
     * */
    public void getConfigure(){
    	MD5_KEY = baseServices.getBaseByBkey(JkConstants.JK_MD5_KEY);
    	URL = baseServices.getBaseByBkey(JkConstants.JK_SERVICE_URL);
    	PARTNER_CODE = baseServices.getBaseByBkey(JkConstants.JK_PARTNER_CODE);
    	/*
        TMitServiceDicBase baseInfo = baseService.selectById(JkConstants.JK_MD5_KEY);
        if (baseInfo!=null){ 
            MD5_KEY= baseInfo.getBvalue().trim();
        }
        
        TMitServiceDicBase baseInfo1 = baseService.selectById(JkConstants.JK_SERVICE_URL);
        if (baseInfo1!=null){  
            URL=baseInfo1.getBvalue().trim();
        }
        
        TMitServiceDicBase baseInfo2 = baseService.selectById(JkConstants.JK_PARTNER_CODE);
        if (baseInfo2!=null){
            PARTNER_CODE=baseInfo2.getBvalue().trim();  
        }*/
        
    }

    /**
     * 检查签名是否合法
     * 
     * @param xmlMessageDTO
     * @return
     */
    public boolean checkSign(XmlMessageDTO xmlMessageDTO) {
        getConfigure();
        String sign = DigestUtils.md5Hex(MD5_KEY + xmlMessageDTO.getContent());
        if (sign.equals(xmlMessageDTO.getSign())) {
            return true;
        }
        return false;
    }

    /**
     * 提交请求
     * 
     * @param reqDTO
     *            请求信息
     * @return 响应内容
     */
    public  String postRequest(ReqDTO<?> reqDTO,String who,String where,String what,String attach) throws Exception{
    	String responseStr = null;
    	try {
	        getConfigure();
	        String content = JSON.toJSONString(reqDTO);
	        String sign = DigestUtils.md5Hex(MD5_KEY + content);
	        String postXmlStr = XmlMessageUtil.createXml(PARTNER_CODE, sign, content);
	        blogout.info("请求金科报文:"+postXmlStr);
	        JSONObject json = XmlUtil.xml2JSON(postXmlStr);
	        BlogUtil.outlog(json.toJSONString(), who, where, what, "req", attach);
            responseStr = HttpUtils.request(postXmlStr, URL, 10 * 1000);
            blogout.info("金科返回报文:" +responseStr);
            JSONObject jsonResp = XmlUtil.xml2JSON(responseStr);
            BlogUtil.outlog(jsonResp.toJSONString(), who, where, what, "resp", attach);
           
        } catch (Exception e) {
        	 blogout.error("请求异常",e);
        	 throw e;
        }
        return responseStr;
    }

    public String test(){
		return MD5_KEY;
    	
    }
    public String getPARTNER_CODE() {
        return PARTNER_CODE;
    }

    public void setPARTNER_CODE(String pARTNER_CODE) {
        PARTNER_CODE = pARTNER_CODE;
    }

    public String getURL() {
        return URL;
    }

    public void setURL(String uRL) {
        URL = uRL;
    }

    public String getMD5_KEY() {
        return MD5_KEY;
    }

    public void setMD5_KEY(String mD5_KEY) {
        MD5_KEY = mD5_KEY;
    }
    

}
